package madixin.diff;

public class T1109 {
    public int[] corpFlightBookings(int[][] bookings, int n) {
//        输入：bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
//        输出：[10,55,45,25,25]
        int res[] = new int[n];
        for (int[] booking : bookings) {
            res[booking[0] - 1] += booking[2];

            if (booking[1] < n) {
                res[booking[1]] -= booking[2];
            }
        }
        for (int i = 1; i < n; i++) {
            res[i] += res[i - 1];
        }
        return res;
    }

    public int[] corpFlightBookings2(int[][] bookings, int n) {
//        输入：bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
//        输出：[10,55,45,25,25]
        int res[] = new int[n];
        for (int[] booking : bookings) {
            for (int i = booking[0] -1; i <= booking[1] -1; i++) {
                res[i] += booking[2];
            }
        }

        return res;
    }

    public boolean carPooling(int[][] trips, int capacity) {
        int[] diffs = new int[1000];

        // 生成diff差分数组
        for (int trip[] : trips) {
            diffs[trip[1]] += trip[0];
            if (trip[2]< 1000) {
                diffs[trip[2]] -= trip[0];
            }
        }

        // 还原成原始数组
        int curPassengers = 0;
        for (int diff : diffs) {
            curPassengers += diff;
            if (curPassengers > capacity) {
                return false;
            }
        }
        return true;
    }

}
